//
//  HWlingqunzhongxinTableViewCell.m
//   
//
//  Created by 陈道斌 on 2021/3/8.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWlingqunzhongxinTableViewCell.h"
#import "HWlingquanzhongxingCollectionViewCell.h"
#import "HWLoginController.h"

@interface HWlingqunzhongxinTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,HWlingquanzhongxingCollectionViewCellDelegate>

@property(strong,nonatomic) UICollectionView *maincollectionView;

@end

@implementation HWlingqunzhongxinTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.zongshituView.layer.cornerRadius = 10;
    self.zongshituView.layer.masksToBounds = YES;
     
    self.firstView.backgroundColor = [UIColor whiteColor];
    self.secondView.backgroundColor = [ UIColor whiteColor];
    
    self.icoimageView.layer.cornerRadius = 8;
    self.icoimageView.layer.masksToBounds = YES;
    self.icoimageView.backgroundColor = RGB(246, 246, 246);
    
    
    
    self.xianqingBut.layer.cornerRadius = 17;
    self.xianqingBut.layer.masksToBounds = YES;
    
    self.xianqingBut.layer.borderWidth = 1;
    self.xianqingBut.layer.borderColor =  RGB(248, 118, 6).CGColor;
    
    
    [self.xianqingBut  setTitle:@"详情" forState:UIControlStateNormal];
    
    [self.xianqingBut   setTitle:[HWjianzhiduiObject keylocalization:@"详情"] forState:UIControlStateNormal];
    
    
    
    // 设置UI
    [self  setupUI];
    
    
    
    
    
    
    
    
}


// 设置UI
- (void)setupUI  {
    
    
    [self addControlsview];
    
    
    
    
}


#pragma  mark -----  添加控件
- (void)addControlsview {
    
 //    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    
    layout.scrollDirection  = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 10;
    
   // CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
  //   layout.minimumInteritemSpacing = juanju-1;
  //  layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,kScreenW-40-12 , 76) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
   // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
   //  self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
    self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 12, 0, 0);
  //   [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
  //   [self.maincollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWlingquanzhongxingCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    self.maincollectionView.userInteractionEnabled = YES;
    [self.secondView  addSubview:self.maincollectionView];
    
    
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return [self.model.list count];
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWlingquanzhongxingCollectionViewCell   *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
  //   cell.backgroundColor =[UIColor yellowColor];
 //     HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
//     cell.model = mode;
    
   //  cell.backgroundColor = [UIColor redColor];
    
    cell.backgroundColor = [UIColor clearColor];
    
    
    NSDictionary *dic = self.model.list[indexPath.row];
    
  //   cell.jingershuujuLabel.text  = [NSString stringWithFormat:@"%@\n%@",dic[@"jian"],dic[@"type"]];
   NSString *jingestr  = [NSString stringWithFormat:@"￥%@\n%@",dic[@"jian"],dic[@"type"]];
    NSString *manay =  [NSString stringWithFormat:@"%@",dic[@"jian"]];
    
    cell.jingershuujuLabel.attributedText = [WMUtil attributedStringWithText:jingestr withRangText:manay withColor:RGB(248, 119, 6) withFont:[UIFont boldSystemFontOfSize:24]];
    
    
    cell.jingershuujuLabel.numberOfLines = 2;
    
    cell.biaoti1Label.text =[NSString stringWithFormat:@"%@",dic[@"title1"]];
    cell.biaoti2Label.text =[NSString stringWithFormat:@"%@",dic[@"title2"]];
    cell.biaoti3Label.text =[NSString stringWithFormat:@"%@",dic[@"title3"]];
    cell.delegate = self;
    
    
    
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(280,76);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    

     
    
}







- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)setModel:(HWyouhuiqunModel *)model {
    
    _model  =  model;
    
    
    [self.icoimageView  sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:nil];
   
    
    self.minghcnegLabel.text = [NSString stringWithFormat:@"%@",model.name];
    self.jiaosdaxiaoLabel.text = [NSString stringWithFormat:@"%@ %@",model.type ,model.size?model.size:@""];
    if ([model.gid isEqualToString:@"0"]) {
        self.xianqingBut.hidden = YES;
    }
    [self.maincollectionView reloadData];
    
  
    
}


- (void)xuanzhongUICollectionViewCell:(HWlingquanzhongxingCollectionViewCell *)cell {
    
    
    
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        
          HWLoginController  *login = [[HWLoginController alloc]init];
            // [  pushViewController:login animated:YES];
        UIViewController *viewcon =  [HWViewUtils getCurrentVC];
        
         [[viewcon navigationController] pushViewController:login animated:YES];
        
        
        
        
           return;
    
    }
    
    
    
    NSIndexPath *index = [self.maincollectionView indexPathForCell:cell];
    
    NSDictionary *dic = self.model.list[index.row];
    
    
    NSLog(@"%@",dic);
    
    
    
    int  r =  [WMUtil getRandomNumber:101 to:998];
  
  
           NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
           NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],r];
  
  
  
           NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
  
           NSString *singMD5 = [WMUtil md5HexDigest:singStr];
           
    NSString *idstr = [NSString stringWithFormat:@"%@",dic[@"id"]];
  
           NSDictionary *parametes = @{
  
                                              @"ios" : @"1",
                                              @"ct" :  @"app",
                                              @"ac" : @"get_coupons",
                                              @"uid" :uidStr,
                                              @"t" : tStr,
                                              @"sign" : singMD5,
                                              @"member_id":[HWLocallyStoredData getCode],
                                              @"id" : idstr
  
                                              };
  
             [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
  
  
  
  
                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
  
                  if ([codeStr intValue] == 1) {
  
  
  
  
                    
                      [MBProgressHUD showError:response[@"message"]];
  
  
  
                  } else  {
  
                       [MBProgressHUD showError:response[@"message"]];
  
                  }
  
  
  
              } failure:^(NSString *error) {
                  [MBProgressHUD showError:@"兑换失败"];
  
  
              }];
  
  
  
      
    
    
    
    
    
    
}







@end
